home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////////
- // $Id: Tools.cxx,v 1.1 1994/02/18 19:54:12 bmott Exp $
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Tools.cxx - This is a collection of useful routines
- //
- // unsigned int StringToInt(String &);
- // String& IntToString(unsigned int value, int size, int field_width);
- //
- //
- // BSVC "A Microprocessor Simulation Framework"
- // Copyright (c) 1993
- // By: Bradford W. Mott
- // November 6,1993
- //
- ///////////////////////////////////////////////////////////////////////////////
- // $Log: Tools.cxx,v $
- // Revision 1.1 1994/02/18 19:54:12 bmott
- // Initial revision
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- #include <stdio.h>
-
- #include "String.h"
- #include "Tools.hxx"
-
- ///////////////////////////////////////////////////////////////////////////////
- // Convert the string to an unsigned integer
- ///////////////////////////////////////////////////////////////////////////////
- unsigned int StringToInt(String string)
- {
- static char hex_digits[]="0123456789abcdef";
- unsigned int value=0;
-
- // Convert the string to all lower case
- string.downcase();
-
- for(int t=0;t<string.length();++t)
- {
- for(int s=0;s<16;++s)
- {
- if(hex_digits[s] == (char)string[t])
- break;
- }
- if(s==16)
- break;
- else
- value=(value << 4) + s;
- }
- return(value);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // Convert the value to a string with the given width
- ///////////////////////////////////////////////////////////////////////////////
- String IntToString(unsigned int value, int width)
- {
- static char hex_digits[]="0123456789abcdef";
- String string;
- int t,i;
-
- for(t=0;t<width;++t)
- string+="0";
-
- i=string.length()-1;
- for(t=0;t<width;++t)
- {
- string[i]=hex_digits[value&0xf];
- value=value >> 4;
- if((i=i-1) < 0)
- break;
- }
- return(string);
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////
- // Convert the value to a decimal string with the given width
- ///////////////////////////////////////////////////////////////////////////////
- String IntToDecimal(unsigned long value, int width)
- {
- char buffer[80], format[20];
-
- sprintf(format,"%%%dld",width);
- sprintf(buffer,format,value);
-
- String string(buffer);
- return(string);
- }
-
-